package org.tp8.sampleapp.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.tp8.sampleapp.jaxb.adapter.XmlLongAdapter;

/**
 * The Class Course.
 */
@XmlRootElement(name = "course")
@XmlAccessorType(XmlAccessType.FIELD)
public class Course extends AbstractModel<Long> {

	/**
	 * The Constant serialVersionUID.
	 */
	private static final long serialVersionUID = 3865013646954591904L;

	/**
	 * The id.
	 */
	@XmlElement(name = "id")
	@XmlID
	@XmlJavaTypeAdapter(XmlLongAdapter.class)
	private Long id;

	/**
	 * The title.
	 */
	@XmlElement(name = "title")
	private String title;

	/**
	 * The teacher.
	 */
	@XmlElement(name = "teacher")
	private Teacher teacher;

	/**
	 * The students.
	 */
	@XmlElementWrapper(name = "students")
	@XmlElement(name = "student")
	@XmlIDREF
	private List<Student> students;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.model.BaseModel#getId()
	 */
	@Override
	public Long getId() {
		return this.id;
	}

	/**
	 * Gets the students.
	 * 
	 * @return the students
	 */
	public List<Student> getStudents() {
		return this.students;
	}

	/**
	 * Gets the teacher.
	 * 
	 * @return the teacher
	 */
	public Teacher getTeacher() {
		return this.teacher;
	}

	/**
	 * Gets the title.
	 * 
	 * @return the title
	 */
	public String getTitle() {
		return this.title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.model.BaseModel#setId(java.lang.Object)
	 */
	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets the students.
	 * 
	 * @param students
	 *            the new students
	 */
	public void setStudents(final List<Student> students) {
		this.students = students;
	}

	/**
	 * Sets the teacher.
	 * 
	 * @param teacher
	 *            the new teacher
	 */
	public void setTeacher(final Teacher teacher) {
		this.teacher = teacher;
	}

	/**
	 * Sets the title.
	 * 
	 * @param title
	 *            the new title
	 */
	public void setTitle(final String title) {
		this.title = title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((students == null) ? 0 : students.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (obj instanceof Long) {
			Long id = (Long) obj;
			return id.equals(getId());
		}
		if (!(obj instanceof Course)) {
			return false;
		}
		Course other = (Course) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (students == null) {
			if (other.students != null) {
				return false;
			}
		} else if (!students.equals(other.students)) {
			return false;
		}
		if (teacher == null) {
			if (other.teacher != null) {
				return false;
			}
		} else if (!teacher.equals(other.teacher)) {
			return false;
		}
		if (title == null) {
			if (other.title != null) {
				return false;
			}
		} else if (!title.equals(other.title)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Course [id=" + id + ", title=" + title + ", teacher=" + teacher
				+ ", students=" + students + "]";
	}

}
